package com.huawang.ecolink.apps.controllers.app;

import com.huawang.ecolink.apps.service.app.AppReceiveService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

@Log
@Api(tags = "appReceive")
@RestController
@RequestMapping("/mmc")
public class AppReceiveController {

    @Autowired
    private AppReceiveService appReceiveService;


    /*** 
     * @Description: 接收数据并转发
     * @param reqBody String
     * @return: java.lang.String
     * @throws:
     */
    @ApiOperation("接收数据并转发")
    @PostMapping("/appsvr")
    public String appReceiveForward(@RequestBody String reqBody) throws UnsupportedEncodingException {
        String param = URLDecoder.decode(reqBody, "UTF-8");
        log.info("参数：" + param);
        String[] split = param.split("=");
        if (split.length == 2) {

            String params = split[1];
            String[] mdata = params.split("\\|\\|\\|");
            if (mdata[1].startsWith("Boolan") || mdata[1].startsWith("Real") || mdata[1].startsWith("Integer") || mdata[1].startsWith("String")) {
                return appReceiveService.handleTest(mdata[1]);
            } else if (mdata[1].contains(",") || mdata[1].replace("-", "").matches("^[0-9]+$") || mdata[1].replace("-", "").matches("\\d+.\\d+")) {
                return appReceiveService.handle(params);
            } else if (mdata[1].contains("${send}")) {
                appReceiveService.handlerInputSendValue(mdata[0]);
            } else {
                return appReceiveService.handle1(params);
            }


        }
        return null;

    }


}
